package org.java.search.service.impl;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.java.common.util.JsonUtils;
import org.java.item.entity.Sku;
import org.java.search.service.RabbitMqService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author WHQ
 * @version 1.0
 * @date 2022-3-20 0020 下午 3:47
 */
@Service
public class RabbitMqSerivceImpl implements RabbitMqService {
    //注入es执行对象
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Value("${es.index}")
    private String index;
    @RabbitListener(queues = "es_queue")
    @Override
    public void del(Long skuId) {
        //得到搜索请求对象
        SearchRequest searchRequest = new SearchRequest(index);
        //2.构建搜索构建器
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //3.指定匹配的条件
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("id", skuId);
        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //5.将搜索条件添加到搜索请求中
        searchRequest.source(sourceBuilder);
        //6.执行搜索,得到响应实体
        try {
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            String id = searchResponse.getHits().getHits()[0].getId();
            DeleteRequest deleteRequest = new DeleteRequest(index,id);
            DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
            System.out.println(deleteResponse.status());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RabbitListener(queues = "es_update_queue")
    @Override
    public void update(String json) {
        Sku sku = JsonUtils.parse(json, Sku.class);
        Long skuId = sku.getId();
        //得到搜索请求对象
        SearchRequest searchRequest = new SearchRequest(index);
        //2.构建搜索构建器
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //3.指定匹配的条件
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("id", skuId);
        sourceBuilder.query(termQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //5.将搜索条件添加到搜索请求中
        searchRequest.source(sourceBuilder);
        //6.执行搜索,得到响应实体
        try {
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            String id = search.getHits().getHits()[0].getId();
            //根据id更新数据
            UpdateRequest updateRequest = new UpdateRequest(index,id);
            updateRequest.doc(json, XContentType.JSON);
            UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
            System.out.println(update.status());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @RabbitListener(queues = "es_add_queue")
    @Override
    public void add(String json) {
        System.out.println(json);
//        Sku sku = JsonUtils.parse(json, Sku.class);
        IndexRequest request = new IndexRequest(index);
        request.timeout(TimeValue.timeValueSeconds(3));
        request.source(json,XContentType.JSON);
        try {
            IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
            System.out.println(response.status());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
